package com.imooc.poker;

import java.sql.SQLOutput;
import java.util.*;

/**
 * @author duyong
 * @version 1.0
 * @date 2019/10/24 16:02
 */

public class Test {
    //保存扑克牌集合
    private List<HandCards> handCards = new ArrayList<HandCards>();
    //保存玩家集合
    private List<Player> players = new ArrayList<Player>();
    //保存牌对应大小
    List cardSize = new ArrayList();

    /**
     * 创建扑克牌
     */
    public void createHandCards() {
        System.out.println("-------------创建扑克牌----------");
        System.out.println("---------------扑克牌创建成功----------");
        //添加扑克牌颜色
        String[] colours = {"方片", "花片", "红桃", "黑桃"};
        //添加点数
        String[] points = {"2", "3", "4", "5", "6", "7",
                "8", "9", "10", "J", "Q", "K", "A"};
        //创建个接收颜色和字符串拼接list数组

        System.out.print("创建扑克：   ");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {

                 //i+j*13 这里不是唯一的  这里总值要受j的影响大
                handCards.add(new HandCards(colours[i], points[j], i+j*13));
            }
        }
        Test test = new Test();
        test.forEach(handCards);
    }

    /**
     * 洗牌
     */
    public void reform() {
        System.out.println("\n" + "------------------开始洗牌-------------------");
        // 随机打乱list顺序
        Collections.shuffle(handCards);
        System.out.println("------------------洗牌结束-------------------");
    }

    /**
     * 创建玩家
     */
    public void createPlayer() {
        Scanner sc = new Scanner(System.in);
        Player player = new Player();
        System.out.println("\n" + "-------------创建玩家----------");
        System.out.println("请输入第一位玩家的ID和姓名");
        //创建2个人
        int id = 0;
        for (int i = 0; i < 2; i++) {
            System.out.println("请输入ID");
            id = Integer.parseInt(sc.nextLine().trim());
            System.out.println("请输入姓名");
            String name = sc.nextLine().trim();
            Player player1 = new Player(id, name);
            players.add(player1);
        }
        System.out.println("------------欢迎玩家:" + players.get(0).getName());
        System.out.println("——————-欢迎玩家：" + players.get(1).getName());

    }

    /**
     * 开始发牌
     */
    public void startDeal() {
        System.out.println("------------------开始发牌-------------------");
        System.out.println("------玩家：" + players.get(0).getName() + "--拿牌");
        System.out.println("------玩家：" + players.get(1).getName() + "--拿牌");
        System.out.println("------玩家：" + players.get(0).getName() + "--拿牌");
        System.out.println("------玩家：" + players.get(1).getName() + "--拿牌");
        System.out.println("------------------发牌结束-------------------");
    }
    /**
     * 开始
     */
    public void startPlay() {
        System.out.println("------------------开始游戏-------------------");
        //第一个玩家最大牌对应的值
        int firstPlayerMaxSize;
        //第二个玩家最大牌对应的值
        int scoundPlayerMaxSize;
        //利用3目运算符选出最大牌
        String firstMaxCard = handCards.get(0).getSize() >= handCards.get(2).getSize() ?
                this.handCards.get(0).toString() :
                this.handCards.get(2).toString();
        //第一玩家牌对应的值得大小
        if (firstMaxCard == this.handCards.get(0).toString()) {
            firstPlayerMaxSize = handCards.get(0).getSize();
        } else {
            firstPlayerMaxSize = handCards.get(0).getSize();
        }
        System.out.println("玩家" + players.get(0).getName() + "最大的手牌是：" + firstMaxCard);

        String scoundMaxCard = handCards.get(1).getSize() >= handCards.get(3).getSize() ?
                this.handCards.get(1).toString() :
                this.handCards.get(3).toString();

        if (firstMaxCard == this.handCards.get(1).toString()) {
            scoundPlayerMaxSize = handCards.get(1).getSize();
        } else {
            scoundPlayerMaxSize = handCards.get(3).getSize();
        }
        System.out.println("玩家" + players.get(1).getName() + "最大的手牌是：" + scoundMaxCard);
        System.out.println("------------------开始比较-------------------");
         //对2位玩家的最大牌进行比较
        String win = firstPlayerMaxSize > scoundPlayerMaxSize ? players.get(0).getName() : players.get(1).getName();
        System.out.println("玩家 " + win + "胜出");

    }

    /**
     * 打印玩家手牌
     */
    public void printPlayerHandCards() {
        System.out.println("------------------玩家手牌------------------");
        System.out.println(players.get(0).getName() + "[" + this.handCards.get(0).toString()
                + "," + this.handCards.get(2).toString() + "]");
        System.out.println(players.get(1).getName() + "[" + this.handCards.get(1).toString()
                + "," + this.handCards.get(3).toString() + "]");
    }
    

    /**
     * 打印集合
     * @param o
     */
    public void forEach(List o) {
        for (Object o1 : o) {
            HandCards handCards = (HandCards) o1;
            System.out.print(handCards.getColour() + handCards.getPoint() + "  ");
        }
    }

    public static void main(String[] args) {

        Test test = new Test();
        test.createHandCards();
        test.reform();
        test.createPlayer();
        test.startDeal();
        test.startPlay();
        test.printPlayerHandCards();
    }
}
